<?php
global $_W, $_GPC;
$weid = $this->_weid;
$from_user = $this->_fromuser;
$setting = $this->getSetting();
$cur_nave = 'marketingorder';
$status = 0;
$storeid = intval($_GPC['storeid']);

if (!empty($_GPC['status'])) {
    $status = intval($_GPC['status']);
}

$op = $_GPC['op'];

//市场专员
$marketing = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketing') . " WHERE weid = :weid AND from_user=:from_user LIMIT 1;", array(':weid' => $this->_weid, ':from_user' => $from_user));
if (empty($marketing)) {
    message('您不是市场专员，无权访问！');
}
$do = 'marketingorder';

if ($setting['auth_mode'] == 1 || empty($setting)) {
    $method = 'order'; //method
    $host = $this->getOAuthHost();
    $authurl = $host . 'app/' . $this->createMobileUrl($method, array('op' => $op, 'storeid' => $storeid), true) . '&authkey=1';
    $url = $host . 'app/' . $this->createMobileUrl($method, array('op' => $op, 'storeid' => $storeid), true);
    if (isset($_COOKIE[$this->_auth2_openid])) {
        $from_user = $_COOKIE[$this->_auth2_openid];
        $nickname = $_COOKIE[$this->_auth2_nickname];
        $headimgurl = $_COOKIE[$this->_auth2_headimgurl];
    } else {
        if (isset($_GPC['code'])) {
            $userinfo = $this->oauth2($authurl);
            if (!empty($userinfo)) {
                $from_user = $userinfo["openid"];
                $nickname = $userinfo["nickname"];
                $headimgurl = $userinfo["headimgurl"];
            } else {
                message('授权失败!');
            }
        } else {
            if (!empty($this->_appsecret)) {
                $this->getCode($url);
            }
        }
    }
} else {
    load()->model('mc');
    if (empty($_W['fans']['nickname'])) {
        mc_oauth_userinfo();
    }
    $from_user = $_W['fans']['openid'];
    $nickname = $_W['fans']['nickname'];
    $headimgurl = $_W['fans']['tag']['avatar'];
}


if (empty($from_user)) {
    message('会话已过期，请重新发送关键字!');
}

$storelist = pdo_fetchall("SELECT * FROM " . tablename($this->table_stores) . " WHERE weid=:weid ORDER BY id DESC ", array(':weid' => $weid), 'id');

$strwhere = " AND a.status=3 AND a.ispay=1 AND a.marketingid={$marketing['id']} ";

$zero_time = mktime(0, 0, 0);
if ($op == 'today') {
    $title = '今日订单';
    $strwhere .= " AND a.dateline>{$zero_time} ";
} else if ($op == 'nowmonth') {
    $title = '本月订单';
    $strwhere .= " AND DATE_FORMAT(FROM_UNIXTIME(a.dateline),'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') ";
} else if ($op == 'lastmonth') {
    $title = '上月订单';
    $strwhere .= " AND PERIOD_DIFF( date_format( now( ) , '%Y%m' ) , date_format(FROM_UNIXTIME(a.dateline), '%Y%m' ) ) =1 ";
}

$order_list = pdo_fetchall("SELECT a.* FROM " . tablename($this->table_order) . " AS a LEFT JOIN " . tablename($this->table_stores) . " AS b ON a.storeid=b
.id  WHERE a.weid = '{$weid}' AND a.storeid={$storeid} AND a.marketingid='{$marketing['id']}' {$strwhere}  ORDER BY a.id DESC LIMIT 1000");



include $this->template($this->cur_tpl . '/marketingorder');